package com.example.api.job;

import com.alibaba.fastjson.JSON;
import com.example.api.dao.ScheduleTaskDao;
import com.example.api.model.constants.LogisticsConstants;
import com.example.api.model.entity.ScheduleTask;
import com.example.api.service.TimeTaskService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;

import javax.annotation.Resource;
import java.util.Objects;

@Configuration
@EnableScheduling
public class LogisticsTask implements SchedulingConfigurer {

    private static final Logger logger = LoggerFactory.getLogger(LogisticsTask.class);

    @Resource
    private ScheduleTaskDao scheduleTaskDao;

    @Resource
    private TimeTaskService timeTaskService;

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.addTriggerTask(
                () -> {
                    logger.info("定时删除逾期登录验证码----开始===============>");
                    try {
                        String method = "com.example.api.service.TimeTaskService.taskDeleteExpireCode";
                        ScheduleTask task = scheduleTaskDao.selectByMethod(method);
                        logger.info("cron表达式：{}", JSON.toJSON(task));
                        if (Objects.isNull(task) || LogisticsConstants.SCHEDULE_TASK_STATUS_0.equals(task.getStatus())) {
                            logger.error("定时删除逾期登录验证码未配置或未开启");
                            return;
                        }
                        timeTaskService.taskDeleteExpireCode();
                    } catch (Exception e) {
                        logger.error("定时删除逾期登录验证码----异常：{} ", e.getMessage());
                        e.printStackTrace();
                    }
                    logger.info("定时删除逾期登录验证码----结束===============>");
                },
                triggerContext -> {
                    String method = "com.example.api.service.TimeTaskService.taskDeleteExpireCode";
                    String cron = scheduleTaskDao.selectByMethod(method).getCron();
                    logger.info("cron表达式：{} ", cron);
                    if (StringUtils.isBlank(cron)) {
                        logger.info("cron表达式为空");
                        cron = "0 0/5 * * * ?";
                    }
                    return new CronTrigger(cron).nextExecutionTime(triggerContext);
                }
        );

        scheduledTaskRegistrar.addTriggerTask(
                () -> {
                    logger.info("定时更新逾期未支付收费信息状态----开始===============>");
                    try {
                        String method = "com.example.api.service.TimeTaskService.taskUpdateExpireToll";
                        ScheduleTask task = scheduleTaskDao.selectByMethod(method);
                        logger.info("cron表达式：{}", JSON.toJSON(task));
                        if (Objects.isNull(task) || LogisticsConstants.SCHEDULE_TASK_STATUS_0.equals(task.getStatus())) {
                            logger.error("定时更新逾期未支付收费信息状态未配置或未开启");
                            return;
                        }
                        timeTaskService.taskUpdateExpireToll();
                    } catch (Exception e) {
                        logger.error("定时更新逾期未支付收费信息状态----异常：{} ", e.getMessage());
                        e.printStackTrace();
                    }
                    logger.info("定时更新逾期未支付收费信息状态----结束===============>");
                },
                triggerContext -> {
                    String method = "com.example.api.service.TimeTaskService.taskUpdateExpireToll";
                    String cron = scheduleTaskDao.selectByMethod(method).getCron();
                    logger.info("cron表达式：{} ", cron);
                    if (StringUtils.isBlank(cron)) {
                        logger.info("cron表达式为空");
                        cron = "0 0/5 * * * ?";
                    }
                    return new CronTrigger(cron).nextExecutionTime(triggerContext);
                }
        );
    }

}
